Skip to content

Incorporate recordPlot functionality for recording and replaying plots as objects - #686

Open
grantmcdermott wants to merge 1 commit into
mainfrom
recordPlot
Open

Incorporate recordPlot functionality for recording and replaying plots as objects#686
grantmcdermott wants to merge 1 commit into
mainfrom
recordPlot

Conversation

@grantmcdermott

@grantmcdermott grantmcdermott commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Fixes #121

By adding grDevices::recordPlot() to the very end of the main tinyplot() function, we gain the ability to record (assign) plots as objects and then replay them later.

pkgload::load_all("~/Documents/Projects/tinyplot/")
#> ℹ Loading tinyplot

a = plt(Sepal.Length ~ Petal.Length | Species, data = iris)

plt(1) # some other random plot

a  # replay the saved plot

Here's a little plt_add gotcha, though...

plt_add(type = "lm") # gotcha: the last plot "call" was plt(1)!

But note that nothing stops us from layering immediately... and we can record and replay from a later layer too. Here, demonstrating with a theme to drive home the point.

plt(Sepal.Length ~ Petal.Length | Species, data = iris, theme = "socviz")
b = plt_add(type = "lm")

plt(1) # random placeholder again

b # replay the saved, layered plot object

Note: Due to the "canvas" nature of base graphics, we can't suppress the initial display. Unless, that is, we do something tricksy like write to a dummy device / file on disk and then recall later.

png("dummyplot.png")
dev.control("enable") # default for png is "inhibit" -> disallows recording
p = plt(Nile, type = "hist")
dev.off()
#> png 
#>   2
p # replay directly on our main, interactie device

Created on 2026-08-20 with reprex v2.1.1

Discussion: To be clear, I'm not convinced that we want to add this functionality. But some quick testing, at least, suggests that it doesn't add any real overhead. Apart from the plt_add() gotcha that I highlight above, I also haven't hit any unintended side effects. Still, I'd like others to kick the tyres too, before we consider whether merging is a good idea.

@b-rodrigues

Copy link
Copy Markdown

Can confirm it works :)
image

see: https://github.com/b-rodrigues/test-tinyplot/tree/master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wishlist: Will tinyplot ever support returning a plot object as ggplot2 does?

2 participants